package com.example.mscluster.provider.order;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.mscluster.provider.order.dao.User2Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

@RestController
public class OrderProviderController {

    @Autowired
    private User2Mapper user2Mapper;

    @GetMapping("/ping")
    public String info() {
        return "pong";
    }

    @GetMapping("/provider/order/all")
    public String providerAllOrder() {
        List<User2> user2List = user2Mapper.selectList(Wrappers.<User2>lambdaQuery().select(User2::getId,  User2::getName, User2::getAge));
        sleepRandomSec();
        return GsonUtils.object2Json(user2List);
    }

    public void sleepRandomSec() {
        Random rand = new Random();
        long sleepMill=Math.round(Math.abs(rand.nextGaussian()*100));
        try {
            System.out.println("sleep "+sleepMill+" millsec!");
            Thread.sleep(sleepMill);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
